package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.brand.DashBrand;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.common.User;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author stone
 * @date 2022/06/25 11:11
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;


    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;

    @Override
    public CommonData<MarketUser> query(MarketUser marketUser,BasePageInfo info) {
        //传入页码，每页个数
        PageHelper.startPage(info.getPage(),info.getLimit());
        //创建example
        MarketUserExample marketUserExample = new MarketUserExample();
        //把排序条件传入
        marketUserExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        //
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (marketUser.getUsername() != null && !"".equals(marketUser.getUsername())) {
            criteria.andUsernameLike("%" + marketUser.getUsername() + "%");
        }
        if (marketUser.getMobile() != null && !"".equals(marketUser.getMobile())) {
            criteria.andMobileLike("%" + marketUser.getMobile() + "%");
        }
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);

        PageInfo<MarketUser> marketUserPageInfo = new PageInfo<>(marketUsers);
        Integer total = (int) marketUserPageInfo.getTotal();
        Integer pages = marketUserPageInfo.getPages();

        CommonData<MarketUser> marketUserCommonData = new CommonData<>(total, pages, info.getLimit(), info.getPage(), marketUsers);
        return marketUserCommonData;
    }

    @Override
    public CommonData<MarketAddress> queryAddress(MarketAddress marketAddress,BasePageInfo info) {
        //传入页码，每页个数
        PageHelper.startPage(info.getPage(),info.getLimit());
        //创建example
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        //把排序条件传入
        marketAddressExample.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if (marketAddress.getName() != null && !"".equals(marketAddress.getName())) {
            criteria.andNameLike("%" + marketAddress.getName() + "%");
        }
        if (marketAddress.getUserId() != null && !"".equals(marketAddress.getUserId())) {
            criteria.andUserIdEqualTo(marketAddress.getUserId());
        }

        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);

        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(marketAddresses);
        Integer total = (int) marketAddressPageInfo.getTotal();
        Integer pages = marketAddressPageInfo.getPages();

        CommonData<MarketAddress> marketAddressCommonData = new CommonData<>(total, pages, info.getLimit(), info.getPage(), marketAddresses);
        return marketAddressCommonData;
    }

    @Override
    public CommonData<MarketCollect> queryCollect(MarketCollect marketCollect, BasePageInfo info) {
        //传入页码，每页个数
        PageHelper.startPage(info.getPage(),info.getLimit());
        //创建example
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        //把排序条件传入
        marketCollectExample.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if (marketCollect.getUserId() != null && !"".equals(marketCollect.getUserId())) {
            criteria.andUserIdEqualTo(marketCollect.getUserId());
        }
        if (marketCollect.getValueId() != null && !"".equals(marketCollect.getValueId())) {
            criteria.andValueIdEqualTo(marketCollect.getValueId());
        }

        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);

        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(marketCollects);
        Integer total = (int) marketCollectPageInfo.getTotal();
        Integer pages = marketCollectPageInfo.getPages();

        CommonData<MarketCollect> marketCollectCommonData = new CommonData<>(total, pages, info.getLimit(), info.getPage(), marketCollects);
        return marketCollectCommonData;
    }

    @Override
    public CommonData<MarketFootprint> queryFootprint(MarketFootprint marketFootprint, BasePageInfo info) {
        //传入页码，每页个数
        PageHelper.startPage(info.getPage(),info.getLimit());
        //创建example
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        //把排序条件传入
        marketFootprintExample.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        if (marketFootprint.getUserId() != null && !"".equals(marketFootprint.getUserId())) {
            criteria.andUserIdEqualTo(marketFootprint.getUserId());
        }
        if (marketFootprint.getGoodsId() != null && !"".equals(marketFootprint.getGoodsId())) {
            criteria.andGoodsIdEqualTo(marketFootprint.getGoodsId());
        }

        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);

        PageInfo<MarketFootprint> marketFootprintPageInfo = new PageInfo<>(marketFootprints);
        Integer total = (int) marketFootprintPageInfo.getTotal();
        Integer pages = marketFootprintPageInfo.getPages();

        CommonData<MarketFootprint> marketFootprintCommonData = new CommonData<>(total, pages, info.getLimit(), info.getPage(), marketFootprints);
        return marketFootprintCommonData;
    }

    @Override
    public CommonData<MarketSearchHistory> queryHistory(MarketSearchHistory marketSearchHistory, BasePageInfo info) {
        //
        PageHelper.startPage(info.getPage(),info.getLimit());
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        //
        marketSearchHistoryExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        //
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        //
        if (marketSearchHistory.getUserId() != null && !"".equals(marketSearchHistory.getUserId())) {
            criteria.andUserIdEqualTo(marketSearchHistory.getUserId());
        }
        if (marketSearchHistory.getKeyword() != null && !"".equals(marketSearchHistory.getKeyword())) {
            criteria.andKeywordLike("%" + marketSearchHistory.getKeyword() + "%");
        }
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);

        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(marketSearchHistories);
        Integer pages = marketSearchHistoryPageInfo.getPages();
        Integer total = (int)marketSearchHistoryPageInfo.getTotal();
        CommonData<MarketSearchHistory> marketSearchHistoryCommonData = new CommonData<>(total, pages, info.getLimit(), info.getPage(), marketSearchHistories);
        return marketSearchHistoryCommonData;
    }

    @Override
    public CommonData<MarketFeedback> queryFeedback(MarketFeedback marketFeedback, BasePageInfo info) {
        //
        PageHelper.startPage(info.getPage(),info.getLimit());
        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        //
        marketFeedbackExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        //
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();
        //
        if (marketFeedback.getId() != null && !"".equals(marketFeedback.getId())) {
            criteria.andIdEqualTo(marketFeedback.getId());
        }
        if (marketFeedback.getUsername() != null && !"".equals(marketFeedback.getUsername())) {
            criteria.andUsernameLike("%" + marketFeedback.getUsername() + "%");
        }

        List<MarketFeedback> marketFeedbacks = marketFeedbackMapper.selectByExample(marketFeedbackExample);
        PageInfo<MarketFeedback> marketFeedbackPageInfo = new PageInfo<>(marketFeedbacks);
        Integer pages = marketFeedbackPageInfo.getPages();
        Integer total = (int)marketFeedbackPageInfo.getTotal();
        CommonData<MarketFeedback> marketFeedbackCommonData = new CommonData<>(total, pages, info.getLimit(), info.getPage(), marketFeedbacks);
        return marketFeedbackCommonData;
    }

    @Override
    public MarketUser queryById(MarketUser marketUser) {
//        MarketUserExample marketUserExample = new MarketUserExample();
//        marketUserExample.createCriteria().andIdEqualTo(marketUser.getId());
//        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        MarketUser marketUser1 = marketUserMapper.selectByPrimaryKey(marketUser.getId());
        //MarketUser marketUser1 = marketUsers.get(0);
        return marketUser1;
    }

    @Override
    public int update(MarketUser marketUser) {
        marketUser.setUpdateTime(new Date());
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.createCriteria().andIdEqualTo(marketUser.getId());
        int i = marketUserMapper.updateByExample(marketUser, marketUserExample);
        return i;
    }

    @Override
    public DashBrand queryTotal() {
        Integer l = (int)marketUserMapper.countByExample(null);
        Integer l1 = (int)marketGoodsMapper.countByExample(null);
        Integer l2 = (int)marketGoodsProductMapper.countByExample(null);
        Integer l3 = (int) marketOrderMapper.countByExample(null);

        return new DashBrand(l1,l,l2,l3);
    }
}
